@@ -30,6 +30,7 @@ gem 'kramdown' |
||
30 | 30 |
gem "typhoeus" |
31 | 31 |
gem 'nokogiri' |
32 | 32 |
gem 'wunderground' |
33 |
+gem 'forecast_io' |
|
33 | 34 |
gem 'rturk' |
34 | 35 |
|
35 | 36 |
gem "twitter" |
@@ -95,6 +95,10 @@ GEM |
||
95 | 95 |
multipart-post (~> 1.1) |
96 | 96 |
fastercsv (1.5.5) |
97 | 97 |
ffi (1.9.0) |
98 |
+ forecast_io (2.0.0) |
|
99 |
+ faraday |
|
100 |
+ hashie |
|
101 |
+ multi_json |
|
98 | 102 |
foreman (0.63.0) |
99 | 103 |
dotenv (>= 0.7) |
100 | 104 |
thor (>= 0.13.6) |
@@ -279,6 +283,7 @@ DEPENDENCIES |
||
279 | 283 |
dotenv-rails |
280 | 284 |
em-http-request |
281 | 285 |
fastercsv |
286 |
+ forecast_io |
|
282 | 287 |
foreman |
283 | 288 |
geokit-rails3 |
284 | 289 |
jquery-rails |
@@ -0,0 +1,107 @@ |
||
1 |
+require 'forecast_io' |
|
2 |
+require 'date' |
|
3 |
+ |
|
4 |
+module Agents |
|
5 |
+ class ForecastAgent < Agent |
|
6 |
+ cannot_receive_events! |
|
7 |
+ |
|
8 |
+ description <<-MD |
|
9 |
+ The ForecastAgent creates an event for the following day's weather at a given `latitude` and `longitude`. |
|
10 |
+ |
|
11 |
+ You also must select which `day` you would like to get the weather for where the number 0 is for today and 1 is for tomorrow and so on. Weather is only returned for 1 week at a time. |
|
12 |
+ |
|
13 |
+ You must setup an [API key for Forecast](https://developer.forecast.io/) in order to use this Agent. |
|
14 |
+ MD |
|
15 |
+ |
|
16 |
+ event_description <<-MD |
|
17 |
+ Events look like this: |
|
18 |
+ |
|
19 |
+ { |
|
20 |
+ "time" : 1391320800, |
|
21 |
+ "summary" : "Rain throughout the day.", |
|
22 |
+ "icon" : "rain", |
|
23 |
+ "sunriseTime" : "06:48", |
|
24 |
+ "sunsetTime" : "17:16", |
|
25 |
+ "moonPhase" : 0.11, |
|
26 |
+ "precipIntensity" : 0.0582, |
|
27 |
+ "precipIntensityMax" : 0.1705, |
|
28 |
+ "precipIntensityMaxTime" : "21:00", |
|
29 |
+ "precipProbability" : 1, |
|
30 |
+ "precipType" : "rain", |
|
31 |
+ "temperatureMin" : 35.67, |
|
32 |
+ "temperatureMinTime" : "23:00", |
|
33 |
+ "temperatureMax" : 50.82, |
|
34 |
+ "temperatureMaxTime" : "02:00", |
|
35 |
+ "apparentTemperatureMin" : 26.11, |
|
36 |
+ "apparentTemperatureMinTime" : "23:00", |
|
37 |
+ "apparentTemperatureMax" : 50.82, |
|
38 |
+ "apparentTemperatureMaxTime" : "02:00", |
|
39 |
+ "dewPoint" : 41, |
|
40 |
+ "humidity" : 0.92, |
|
41 |
+ "windSpeed" : 4.59, |
|
42 |
+ "windBearing" : 358, |
|
43 |
+ "visibility" : 5.92, |
|
44 |
+ "cloudCover" : 0.99, |
|
45 |
+ "pressure" : 1017.66, |
|
46 |
+ "ozone" : 278.6 |
|
47 |
+ } |
|
48 |
+ |
|
49 |
+ MD |
|
50 |
+ |
|
51 |
+ default_schedule "8pm" |
|
52 |
+ |
|
53 |
+ def working? |
|
54 |
+ event_created_within?(2) && !recent_error_logs? |
|
55 |
+ end |
|
56 |
+ |
|
57 |
+ def key_setup? |
|
58 |
+ options['api_key'] && options['api_key'] != "your-key" |
|
59 |
+ end |
|
60 |
+ |
|
61 |
+ def default_options |
|
62 |
+ { |
|
63 |
+ 'api_key' => "your-key", |
|
64 |
+ 'latitude' => "36.166667", |
|
65 |
+ 'longitude' => "-86.783333", |
|
66 |
+ 'day' => "0" |
|
67 |
+ } |
|
68 |
+ end |
|
69 |
+ |
|
70 |
+ def validate_options |
|
71 |
+ errors.add(:base, "latitude is required") unless options['latitude'].present? |
|
72 |
+ errors.add(:base, "longitude is required") unless options['longitude'].present? |
|
73 |
+ errors.add(:base, "api_key is required") unless options['api_key'].present? |
|
74 |
+ errors.add(:base, "day selection is required") unless options['day'].present? |
|
75 |
+ end |
|
76 |
+ |
|
77 |
+ def check |
|
78 |
+ if key_setup? |
|
79 |
+ ForecastIO.api_key = options['api_key'] |
|
80 |
+ ForecastIO.forecast(options['latitude'],options['longitude']).daily.each do |key, value| |
|
81 |
+ if key == "data" |
|
82 |
+ value.each do |day| |
|
83 |
+ if day_diff(day.time) == options['day'].tp_i |
|
84 |
+ day.sunriseTime = Time.at(day.sunriseTime).strftime("%H:%M") |
|
85 |
+ day.sunsetTime = Time.at(day.sunsetTime).strftime("%H:%M") |
|
86 |
+ day.precipIntensityMaxTime = Time.at(day.precipIntensityMaxTime).strftime("%H:%M") |
|
87 |
+ day.temperatureMinTime = Time.at(day.temperatureMinTime).strftime("%H:%M") |
|
88 |
+ day.temperatureMaxTime = Time.at(day.temperatureMaxTime).strftime("%H:%M") |
|
89 |
+ day.apparentTemperatureMinTime = Time.at(day.apparentTemperatureMinTime).strftime("%H:%M") |
|
90 |
+ day.apparentTemperatureMaxTime = Time.at(day.apparentTemperatureMaxTime).strftime("%H:%M") |
|
91 |
+ create_event :payload => day |
|
92 |
+ end |
|
93 |
+ end |
|
94 |
+ end |
|
95 |
+ end |
|
96 |
+ end |
|
97 |
+ end |
|
98 |
+ |
|
99 |
+ def day_diff(day) |
|
100 |
+ a=Time.at(day).to_date |
|
101 |
+ b=Time.now.to_date |
|
102 |
+ days=(a-b).to_i |
|
103 |
+ return days |
|
104 |
+ end |
|
105 |
+ |
|
106 |
+ end |
|
107 |
+end |